<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\AdvInvite;
use App\Models\AdvSignUp;
use App\Models\Balance;
use App\Models\Enum;
use App\Models\ProducerAdv;
use App\Models\ProducerAdvSnapshot;
use App\Models\UnionOrder;
use App\Models\User;
use App\Services\BalanceService;
use Illuminate\Http\Request;

class SignUpController extends BaseController
{

    public function index(Request $request) {
        $input = $request->input();
        $search = ['status' => -1, 'start_at' => '', 'end_at' => ''];
        $title = $request->input('title');
        $adv_sn = $request->input('adv_sn');
        $list = AdvSignUp::when($title, function ($q) use($title){
            $q->whereHas('adv', function ($qt) use($title){
                $qt->where('title', 'like', '%' . $title . '%');
            });
        })->when($adv_sn, function ($q) use($adv_sn){
            $q->whereHas('adv', function ($qt) use($adv_sn){
                $qt->where('adv_sn', 'like', '%' . $adv_sn . '%');
            });
        })->apply($search, $input)->withCertain('adv.merchant', ['fac_title'])->orderBy('id', 'desc')->paginate(per_page());
        return $this->paginate($list);
    }

    public function order_info(Request $request) {
        $this->validate($request, [
            'order_id' => 'required'
        ]);
        $order_id = $request->input('order_id');
        $info = AdvSignUp::with('adv.merchant')->with('daren')->find($order_id);
        if (!$info)
            return $this->failed('不存在');

        $arr = $info->toArray();
        $arr['adv']['type'] =  Enum::getValueDisplay($arr['adv']['type']);
        return $this->success($arr);
    }

    public function remark(Request $request) {
        $this->validate($request, [
            'order_id' => 'required',
            'remark' => 'required',
        ]);
        $order_id = $request->input('order_id');
        $model = AdvSignUp::find($order_id);
        $model->remark = $request->input('remark');
        $model->save();
        return $this->success(['id' => $order_id], '备注成功');
    }

    public function amount(Request $request) {
        $this->validate($request, [
            'order_id' => 'required',
            'amount' => 'required',
        ]);
        try {
            $amount = $request->input('amount') * 100;
            $order_id = $request->input('order_id');
            $model = AdvSignUp::find($order_id);
            if ($model->status == 1)
                return $this->failed('请勿重复提交结算');
            return \DB::transaction(function () use($amount, $model, $order_id){
                $model->amount = $amount;
                $model->status = 1;
                $model->save();
                $adv = ProducerAdvSnapshot::find($model->adv_snapshot_id);
                $title = $adv->title ?? '';
                BalanceService::store(User::BENEFIT_DAREN, $model->producer_id, $model->merchant_id, $amount, Balance::TYPE_ADV, $model->id, $model->order_sn, $title, false);

                //流水
                UnionOrder::create([
                    'user_id' => 0,
                    'order_id' => $order_id,
                    'type' => UnionOrder::TYPE_ADV,
                    'order_sn' => $model->order_sn,
                    'amount' => $amount,
                    'merchant_id' => $model->merchant_id,
                    'created_at' => date('Y-m-d')
                ]);

                return $this->success(['id' => $order_id], '结算成功');
            });
        } catch (\Exception $e) {
            \Log::error($e);
            return $this->failed($e->getMessage());
        }

    }

}

